#include<bits/stdc++.h>
using namespace std;
char a[11],b[11];

int sum(char a[])
{
	int len=strlen(a);
	int s=0;
	for(int i=0;i<len;i++)
		s=s*10+a[i]-'0';
	return s;
}

int main()
{
	cin>>a>>b;
	sort(a,a+strlen(a));
	int mx=0,flag=0;
	do
	{
		if(a[0]=='0')
			goto loop;
		if(strcmp(a,b)<=0)
		{
			mx=max(mx,sum(a));
			flag=1;
		}
		loop:;
	}while(next_permutation(a,a+strlen(a)));
	if(flag==1)
		cout<<mx;
    else
    	cout<<"-1";
	return 0;
}
